import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.UUID;

/**
 * Copyright © 2018胡建松. All rights reserved.
 *
 * @Package: PACKAGE_NAME
 * @author: hujiansong
 * @date: 2018/7/24 9:53
 * @version: V1.0
 */
public class MainUI extends JFrame implements ActionListener {

    private JPanel jPanel1;
    private JTextField jTextField;
    private JLabel jLabel1;


    private JPanel jPanel2;
    private JComboBox<String> comboBox;
    private JButton jconvert;

    private JPanel jPanel3;
    private JLabel jLabelResultTip;
    private JLabel jLabelResult;


    public MainUI() {
        //初始化所有空间
        initView();
        // 初始化父控件
        initContiner();

        //初始化事件
        initEvent();
    }

    private void initEvent() {
        jconvert.addActionListener(this);
    }

    private void initContiner() {
        this.add(jPanel1);
        this.add(jPanel2);
        this.add(jPanel3);
        this.setLayout(new FlowLayout(FlowLayout.LEFT));            //选择GridLayout布局管理器
        this.setLayout(new GridLayout(3, 1));            //选择GridLayout布局管理器
        this.setTitle("字符串转换");
        this.setSize(400, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //设置当关闭窗口时，保证JVM也退出
        this.setLocation(500, 300);
        this.setVisible(true);
        this.setResizable(true);
        this.setResizable(false);
    }

    private void initView() {
        jPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        jLabel1 = new JLabel("请输入字符串:");
        jPanel1.add(jLabel1);

        jTextField = new JTextField(10);
        jPanel1.add(jTextField);


        jPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        initCombox();
        jconvert = new JButton("转换");
        jconvert.setSize(20, 20);
        jPanel2.add(jconvert);

        jPanel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        jLabelResultTip = new JLabel("转换后的结果:");
        jLabelResult = new JLabel("");
        jPanel3.add(jLabelResultTip);
        jPanel3.add(jLabelResult);
    }

    private void initCombox() {
        comboBox = new JComboBox<>();
        comboBox.addItem("toUpperCase");
        comboBox.addItem("reverse");
        comboBox.addItem("toLowerCase");
        comboBox.addItem("substring");
        comboBox.addItem("charAt");
        comboBox.addItem("append");
        comboBox.addItem("length");
        comboBox.addItem("replace");
        comboBox.addItem("toInt");

        jPanel2.add(comboBox);
    }


    /**
     * 字符串转换的时候必须要有值
     *
     * @param input
     */
    private boolean checkInputStr(String input) {
        if (input == null || "".equals(input)) {
            return false;
        }
        return true;
    }

    private void showBox(String msg) {
        JOptionPane.showMessageDialog(this, msg);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jconvert) {
            String input = jTextField.getText().toString();
            if (!checkInputStr(input)) {
                showBox("请输入字符串！");
                return;
            }

            String command = comboBox.getSelectedItem().toString();
            if ("toUpperCase".equals(command)) {
                String upperCase = input.toUpperCase();
                jLabelResult.setText(upperCase);
            } else if ("reverse".equals(command)) {
                String reverse = new StringBuffer(input).reverse().toString();
                jLabelResult.setText(reverse);
            } else if ("toLowerCase".equals(command)) {
                String toLowerCase = input.toLowerCase();
                jLabelResult.setText(toLowerCase);
            } else if ("substring".equals(command)) {
                int strLen = input.length();
                Random random = new Random(System.currentTimeMillis());
                int i = random.nextInt(strLen - 1);
                String substring = input.substring(i);
                jLabelResult.setText(substring);
            } else if ("charAt".equals(command)) {
                char charAt = input.charAt(0);
                jLabelResult.setText(charAt + "");
            } else if ("append".equals(command)) {
                String uuid = UUID.randomUUID().toString();
                String append = new StringBuffer(input).append(uuid).toString();
                jLabelResult.setText(append + "");
            } else if ("length".equals(command)) {
                int len = input.length();
                jLabelResult.setText("字符长度:" + len);
            } else if ("replace".equals(command)) {
                char firstChar = input.charAt(0);
                String replace = input.replace(firstChar, 'z');
                jLabelResult.setText(replace);
            } else if ("toInt".equals(command)) {
                int i = 0;
                try {
                    i = Integer.parseInt(input);
                } catch (Exception e1) {
                    showBox("无法转换，请重新输入");
                    return;
                }
                jLabelResult.setText(i + "");
            }else{
                showBox("未知操作！");
            }

        }
    }
}
